#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:HeWenjun
# datetime:2023/11/30 9:36
# software: PyCharm
import json

import json


def compare_json(file1, file2):
    # 读取JSON文件
    with open(file1, encoding='utf-8') as f:
        json1 = json.load(f)
    with open(file2, encoding='utf-8') as f:
        json2 = json.load(f)

    # 比较两个JSON对象
    added_items = {}
    missing_items = {}
    diff_items = {}
    # 查找新增项和不同项
    for key in json2:
        if key not in json1:
            added_items[key] = json2[key]
        elif json1[key] != json2[key]:
            diff_items[key] = json2[key]

    # 查找缺少项
    for key in json1:
        if key not in json2:
            missing_items[key] = json1[key]

    # 打印新增项
    if added_items:
        print("B文件中的新增项：")
        for key, value in added_items.items():
            print(f"{key}: {value}")
    else:
        print("B文件中没有新增项。")

    # 打印不同项
    if diff_items:
        print("B文件中不同的键值对：")
        for key, value in diff_items.items():
            print(f"{key}: {value} (与A文件中的值不同)")
    else:
        print("B文件中没有与A文件不同的键值对。")

    # 打印缺少项
    if missing_items:
        print("B文件中的缺少项：")
        for key, value in missing_items.items():
            print(f"{key}: {value}")
    else:
        print("B文件中没有缺少项。")


if __name__ == '__main__':
    # 用法示例
    compare_json("2023-12-04人员列表.json", "2023-12-05人员列表.json")
